package com.moonciki.artadmin.web.commons.config;

import com.moonciki.artadmin.core.constant.BeanConfigConstant;
import com.moonciki.artadmin.core.utils.SnowflakeUtil;
import com.moonciki.artadmin.web.commons.property.SnowflakeProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Configuration
public class WebConfig {

    @Autowired
    private SnowflakeProperty snowflakeProperty;

    /**
     * restTemplate
     * @return
     */
    @Bean
    public RestTemplate restTemplate(){
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置连接超时，单位毫秒
        requestFactory.setReadTimeout(10000);  //设置读取超时
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }

    @Bean
    public SnowflakeUtil snowflakeUtil(){

        SnowflakeUtil idWorker = new SnowflakeUtil(snowflakeProperty.getWorkerId(), snowflakeProperty.getDatacenterId());

        return idWorker;
    }

    /**
     * 系统定时检查线程池
     * @return
     */
    @Bean(BeanConfigConstant.system_scheduled_executor)
    public ScheduledExecutorService systemScheduledExecutor(){
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
        return pool;
    }



}
